<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 */

/**
 * RSS writer
 *
 * PHP version 5
 *
 * LICENSE: This file is part of myLib.
 *
 * myLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * myLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category Writer
 * @package myLib
 * @author Diego Machado de Assis <manejoao@gmail.com>
 * @author Laura de Carvalho Freitas Rodrigues <laura@ccuec.unicamp.br>
 * @copyright 2009 Diego Machado de Assis
 * @license http://www.gnu.org/licenses/gpl.html GNU GENERAL PUBLIC LICENSE Version 3
 * @see xmloutputter.php
 */

require_once 'xmloutputter.php';

/**
 * RSS writer
 *
 * This class implements methods that abstract the written of RSS elements.
 *
 * @category Writer
 * @package myLib
 * @author Diego Machado de Assis <manejoao@gmail.com>
 * @author Laura de Carvalho Freitas Rodrigues <laura@ccuec.unicamp.br>
 * @copyright 2009 Diego Machado de Assis
 * @license http://www.gnu.org/licenses/gpl.html GNU GENERAL PUBLIC LICENSE Version 3
 * @see XMLOutputter
 */
class RSSOutputter extends XMLOutputter {

    /**
     * Constructor
     *
     * Just call the XMLOutputter constructor
     *
     * @param string    $output output file to the writer. Defaults to stdout
     * @param boolean   $indent wheter the output should be indented. Defaults
     *                          to TRUE
     *
     * @access public
     * @see XMLOutputter::__construct
     */
    public function __construct($output='php://output', $indent=true) {
        parent::__construct($output, $indent);
    }

    /**
     * Start the RSS document
     *
     * Starts a XML document and starts the 'rss' and 'channel' tags of the
     * document.
     *
     * @param string    $encoding       encoding of the document. Defaults to
     *                                  'UTF-8'
     * @param string    $xmlVersion     the version of XML document. Defaults to
     *                                  '1.0'
     * @param string    $rssVersion     the version of RSS document. Defaults to
     *                                  '2.0'
     * 
     * @return boolean  TRUE if the document could be succefully started, FALSE
     *                  otherwise
     *
     * @access public
     * @see rssOutputter::RSSEnd()
     */
    public function RSSStart($encoding='UTF-8', $xmlVersion='1.0',
            $rssVersion='2.0') {

        header('content-type: application/rss+xml');

        if( $this->XMLStart($encoding, $xmlVersion) &&
                $this->elementStart('rss', array('version' => $rssVersion) ) &&
                $this->elementStart('channel') ) {
            return true;
        }
        return false;
    }

    /**
     * End the RSS document
     *
     * End the 'rss' and 'channel' tags and calls the XMLOutputter::XMLEnd()
     * method.
     *
     * @return boolean  TRUE on success or FALSE on failure
     *
     * @access public
     * @see rssOutputter::RSSStart()
     */
    public function RSSEnd(){
        return $this->elementEnd('channel') &&
            $this->elementEnd('rss') &&
            $this->XMLEnd();
    }

}
